package com.acme.service.bank.impl;

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.acme.domain.bank.Bank;
import com.acme.domain.bank.Client;
import com.acme.domain.bank.Gender;
import com.acme.exceptions.ClientExistsException;
import com.acme.service.bank.BankService;

public class BankServiceImpl implements BankService {

	public void saveBank(final Bank bank, final String pathTo)
			throws IOException {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(pathTo));
        oos.writeObject(bank);
	}

	public Bank loadBank(final String pathFrom) throws IOException {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(pathFrom));
        try{
            return (Bank)ois.readObject();
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Client addClient(Bank bank, String name, Gender gender) throws ClientExistsException {
        Client client = new Client(name, gender);
        bank.addClient(client);
        return client;
    }


}
